#!/bin/bash
###SHELLPACK preamble thotdata-bench 0

###SHELLPACK parseargBegin
###SHELLPACK parseargInstall
###SHELLPACK parseargParam	--min-threads	THOTDATA_MIN_THREADS
###SHELLPACK parseargParam	--max-threads	THOTDATA_MAX_THREADS
###SHELLPACK parseargEnd
###SHELLPACK monitor_hooks

###SHELLPACK check_install_required thotdata-${VERSION}
###SHELLPACK init_complete

cd $SHELLPACK_SOURCES/thotdata-${VERSION}-installed ||
	die Failed to change to benchmark directory

# Get a list of interleaved CPU between available nodes
TIFS=$IFS
IFS="
"

NODE=0
for LINE in `numactl --hardware | grep cpus: | awk -F ": " '{print $2}'`; do
        echo $LINE | sed -e 's/ /\n/g' > $SHELLPACK_TEMP/interleave.$NODE.$$
        NODE=$((NODE+1))
done

cat $SHELLPACK_TEMP/interleave.*.$$ > $SHELLPACK_TEMP/bynode.$$
paste -d '\n' $SHELLPACK_TEMP/interleave.*.$$ > $SHELLPACK_TEMP/interleave.$$
rm $SHELLPACK_TEMP/interleave.*.$$
IFS=$TIFS

cpupower frequency-set -g performance
for ALIGNMENT in 64 4096 2097152; do
	###SHELLPACK threads_powertwo_begin $THOTDATA_MIN_THREADS $THOTDATA_MAX_THREADS
		CPULIST=`head -$NR_THREADS $SHELLPACK_TEMP/bynode.$$ | tr '\n' ' '`
		MIN_LATENCY=100000
		if [ $ALIGNMENT -eq 2097152 ]; then
			MIN_LATENCY=3500000
		fi
		monitor_pre_hook $LOGDIR_RESULTS $NR_THREADS-$ALIGNMENT
		echo Starting $NR_THREADS/$THOTDATA_MAX_THREADS with alignment $ALIGNMENT
		echo o $CPULIST
		
		$TIME_CMD -o $LOGDIR_RESULTS/threads-${NR_THREADS}-$ALIGNMENT.time	\
			./thotdata $ALIGNMENT $MIN_LATENCY $CPULIST			\
				> $LOGDIR_RESULTS/threads-${NR_THREADS}-$ALIGNMENT.log 2>&1
		monitor_post_hook $LOGDIR_RESULTS $NR_THREADS-$ALIGNMENT
	###SHELLPACK threads_powertwo_end
done
rm $SHELLPACK_TEMP/interleave.$$
rm $SHELLPACK_TEMP/bynode.$$
exit $SHELLPACK_SUCCESS
